package com.shrct.businessprocess.project.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.project.RCCustomerContract;
import com.shrct.businessprocess.project.RCCustomerContractDAO;
import com.shrct.businessprocess.project.RCProject;
import edu.fudan.mylang.pf.BaseDAOService;
import edu.fudan.mylang.pf.IGenericQuery;
import java.util.Collection;
import java.util.List;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ListExtensions;
import org.springframework.stereotype.Service;

@Service("com.shrct.businessprocess.project.RCCustomerContractDAO")
public class RCCustomerContractDAOImpl extends BaseDAOService<RCCustomerContract> implements RCCustomerContractDAO {
  public String getNextContractId(final RCProject project) {
    String _xblockexpression = null;
    {
      Collection<RCCustomerContract> _contracts = project.getContracts();
      final Function1<RCCustomerContract,String> _function = new Function1<RCCustomerContract,String>() {
          public String apply(final RCCustomerContract it) {
            String _contractId = it.getContractId();
            return _contractId;
          }
        };
      List<RCCustomerContract> _sortBy = IterableExtensions.<RCCustomerContract, String>sortBy(_contracts, _function);
      List<RCCustomerContract> _reverse = ListExtensions.<RCCustomerContract>reverse(_sortBy);
      final RCCustomerContract lastContract = IterableExtensions.<RCCustomerContract>head(_reverse);
      int _xifexpression = (int) 0;
      boolean _equals = Objects.equal(lastContract, null);
      if (_equals) {
        String _code = project.getCode();
        int _parseInt = Integer.parseInt(_code);
        int _multiply = (_parseInt * 100);
        int _plus = (_multiply + 1);
        _xifexpression = _plus;
      } else {
        String _contractId = lastContract.getContractId();
        int _parseInt_1 = Integer.parseInt(_contractId);
        int _plus_1 = (_parseInt_1 + 1);
        _xifexpression = _plus_1;
      }
      final int seq = _xifexpression;
      String _string = Integer.valueOf(seq).toString();
      _xblockexpression = (_string);
    }
    return _xblockexpression;
  }
  
  public RCCustomerContract findCustomerContractById(final String contractId) {
    IGenericQuery<RCCustomerContract> query=getObjectFactory().createGenericQuery(RCCustomerContract.class,"select e from com.shrct.businessprocess.project.impl.RCCustomerContractImpl as e   where ((e.contractId) = (:contractId)) and  e.active = 1      ").setParameter("contractId",contractId);
    RCCustomerContract _uniqueResult = query.uniqueResult();
    return _uniqueResult;
  }
}
